package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.impl.dao.SceneCollectDao;
import com.jiantong.lawEnforcing.model.SceneCollect;
import com.jiantong.lawEnforcing.service.SceneCollectService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 现场音影收集(记录现场收集的视频,照片,等)
 * Created by mz03 on 2017/8/25.
 */
@Slf4j
@Service
@RpcProvider
public class SceneCollectServiceImpl implements SceneCollectService {

    private final SceneCollectDao sceneCollectDao;

    @Autowired
    public SceneCollectServiceImpl(SceneCollectDao sceneCollectDao) {
        this.sceneCollectDao = sceneCollectDao;
    }


    /**
     * id 查找
     *
     * @param id
     * @return sceneCollect
     */
    @Override
    public Response<SceneCollect> finById(Long id) {
        try {
            SceneCollect sceneCollect = sceneCollectDao.findById(id);
            if (sceneCollect != null) {
                return Response.ok(sceneCollect);
            } else {
                log.error("sceneCollect (id={}) is not found", id);
                return Response.fail("sceneCollect.not.found");
            }
        }catch (Exception e){
            log.error("failed to find sceneCollect(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("sceneCollect.find.fail");
        }
    }

    /**
     * id 查找
     *
     * @param cases_basic_id
     * @return
     */
    @Override
    public Response<SceneCollect> findByCases_basic_id(Long cases_basic_id) {
        try {
            Map<String,Object> map = Maps.newHashMap();
            if (cases_basic_id != null){
                map.put("cases_basic_id",cases_basic_id);
            }
            List<SceneCollect> list = sceneCollectDao.list(map);
            if (list.size()<=0){
                log.error("sceneCollect (cases_basic_id={}) is not found", cases_basic_id);
                return Response.fail("sceneCollect.not.found");
            }
            SceneCollect sceneCollect = list.get(0);
            if (sceneCollect != null) {
                return Response.ok(sceneCollect);
            } else {
                log.error("sceneCollect (cases_basic_id={}) is not found", cases_basic_id);
                return Response.fail("sceneCollect.not.found");
            }
        }catch (Exception e){
            log.error("failed to find sceneCollect(cases_basic_id={}),cause:{}",cases_basic_id, Throwables.getStackTraceAsString(e));
            return Response.fail("sceneCollect.find.fail");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param sceneCollect 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(SceneCollect sceneCollect) {
        try {
            sceneCollectDao.create(sceneCollect);
            return Response.ok(sceneCollect.getId());
        }catch (Exception e) {
            log.error("failed.to.create.sceneCollect:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.sceneCollect.failed");
        }
    }

    /**
     * 更新新闻的单据
     *
     * @param sceneCollect 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(SceneCollect sceneCollect) {
        try{
            return Response.ok(sceneCollectDao.update(sceneCollect));
        }catch (Exception e){
            log.error("failed.to.update.sceneCollect:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.sceneCollect.failed");
        }
    }

    /**
     * 根据案件id查询相关的音影记录
     *
     * @param pageNo
     * @param pageSize
     * @param cases_basic_id
     * @return
     */
    @Override
    public Response<Paging<SceneCollect>> pagingByCasesBasicId(Integer pageNo, Integer pageSize, Long cases_basic_id) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String,Object> criteria  = Maps.newHashMap();
            if (cases_basic_id != null){
                criteria.put("cases_basic_id",cases_basic_id);
            }
            return Response.ok(sceneCollectDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find pagingSceneCollect(cases_basic_id={}), cause:{}", cases_basic_id, Throwables.getStackTraceAsString(e));
            return Response.fail("pagingSceneCollect.find.fail");
        }
    }
}
